﻿using MediatR;
using Microsoft.EntityFrameworkCore;
using System.Threading;
using Traceability.Commont.Domains.AnimalsDomainFiles;
using Traceability.Commont.DTOs;
using Traceability.Commont.DTOs.CommontFiles;
using Traceability.Commont.Infrastructures.Interfaces;

namespace U.Enterprise.Api.Applications.AdmissionRegistrationFile
{
    /// <summary>
    /// 
    /// </summary>
    public class SlaughteredDetailsInfoHander : IRequestHandler<SlaughteredDetailsInfoCommand, Resqults<SlaughteredDetailsInfoDTO>>
    {
        private readonly IBateRepository<Approach> appro;
        private readonly IBateRepository<LiveAnimalweighing> live;
        private readonly IBateRepository<SlaughteredDetails> slaughteredDetails;
        private readonly IBateRepository<SlaughterCircle> slaughterCircle;

        public SlaughteredDetailsInfoHander(IBateRepository<Approach> appro, IBateRepository<LiveAnimalweighing> live, IBateRepository<SlaughteredDetails> slaughteredDetails, IBateRepository<SlaughterCircle> slaughterCircle)
        {
            this.appro = appro;
            this.live = live;
            this.slaughteredDetails = slaughteredDetails;
            this.slaughterCircle = slaughterCircle;
        }

        public async Task<Resqults<SlaughteredDetailsInfoDTO>> Handle(SlaughteredDetailsInfoCommand request, CancellationToken cancellationToken)
        {
            try
            {
                Resqults<SlaughteredDetailsInfoDTO> res = new Resqults<SlaughteredDetailsInfoDTO>();
                var list =await (from a in appro.GetAll()
                            join b in live.GetAll() on a.ApproachId equals b.ApproachId
                            join c in slaughterCircle.GetAll() on b.SlaughterCircleId equals c.SlaughterCircledId
                            join d in slaughteredDetails.GetAll() on c.SlaughterCircledId equals d.SlaughterCircleId
                            group new { a, b, c ,d} by new
                           {
                               d.ToBeSlaughteredId,
                               a.ApproachBatch,
                               a.Suppliers,
                               a.ShoppingDesignationid,
                               d.SlaughterCircleNum,
                               b.AverageWeight,
                               a.ApproachNo,
                           }
                           into e
                           select new SlaughteredDetailsInfoDTO
                           {
                               ApproachBatch=e.Key.ApproachBatch,
                               Suppliers = e.Key.Suppliers,
                               ShoppingDesignationid = e.Key.ShoppingDesignationid,
                               NumSum = e.Sum(x => x.d.SlaughterCircleNum),
                               AverageWeight = e.Key.AverageWeight,
                               SumAverageWeight = e.Sum(x => x.d.SlaughterCircleNum) * e.Key.AverageWeight,
                               Timenum = Convert.ToDecimal(3.0),
                               Result = "合格",
                               QualityName = "孙倩",
                               QualityTime = DateTime.Now,
                           }).FirstOrDefaultAsync();

                res.objects = list;
                return res;
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
